package com.jiepos.mpos.biz.channel.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.InfChnGrpDao;
import com.jiepos.mpos.common.entity.App_Inf_Chn_GrpEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 通道分组表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.service 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月22日上午10:39:52
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class InfChnGrpService extends BaseService {
	
	@Autowired
	private InfChnGrpDao infChnGrpDao;
	
	/**
	 * 查询通道分组列表json分页数据
	 * @param pagination
	 * @param channel_grp
	 * @return
	 */
	public Map getInfChnGrpList(Pagination pagination,String channel_grp){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.chn_grp_uuid as uuid,NVL2 ( t2.channel_grp||t2.descpt,t2.channel_grp||'-'||t2.descpt, '') as chngrpname,NVL2 ( t3.channel_no||t3.chn_nm,t3.channel_no||'-'||t3.chn_nm, '') as chnname,t1.priority from ");
		sb.append(Table.T_APP_INF_CHN_GRP);
		sb.append(" t1 LEFT JOIN ");
		sb.append(Table.T_APP_INF_CHANNEL_GRP);
		sb.append(" t2 on t1.channel_grp_uuid=t2.channel_grp_uuid ");
		sb.append(" LEFT JOIN ");
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t3 on t1.channel_uuid=t3.channel_uuid ");
		sb.append(" where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(channel_grp)) {
			sb.append( " and t2.channel_grp like ? " );
			list.add("%" + channel_grp + "%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return infChnGrpDao.getPaginationMap(pagination, "t1", list);
	}
	
	
	/**
	 * 保存通道分组
	 * @param infChnGrpEntity
	 * @param map
	 */
	public void saveInfChnGrp(App_Inf_Chn_GrpEntity infChnGrp,Map map){
		infChnGrp.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		if (!StringUtils.isBlank(infChnGrp.getChn_grp_uuid())) {
			App_Inf_Chn_GrpEntity newInfChnGrp =infChnGrpDao.findByParam("chn_grp_uuid", infChnGrp.getChn_grp_uuid());
			infChnGrp.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			infChnGrp.setRec_id(newInfChnGrp.getRec_id());
			infChnGrp.setLast_oper_in(SystemConstant.OPER_UPDATE);
			List noChangeList = new ArrayList();
			noChangeList.add("chn_grp_uuid");
			noChangeList.add("rec_crt_ts");
			infChnGrpDao.update(infChnGrp,noChangeList);
		}else{
			infChnGrp.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			infChnGrp.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			infChnGrp.setChn_grp_uuid(IDFactoryUtil.getUUID());			
			infChnGrp.setRec_st(SystemConstant.NORMAL_STATUS);
			infChnGrp.setLast_oper_in(SystemConstant.OPER_ADD);
			infChnGrpDao.save(infChnGrp);
		}
		
	}
	
	/**
	 * 根据记录id查询实体对象
	 * @param chn_grp_uuid
	 * @return
	 */
	public Map getInfChnGrpDetail(String chn_grp_uuid){
		if (StringUtils.isBlank(chn_grp_uuid) || chn_grp_uuid.equals("undefined")) {
			return new HashMap();
		}
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.chn_grp_uuid,t1.channel_grp_uuid,NVL2 ( t2.channel_grp||t2.descpt,t2.channel_grp||'-'||t2.descpt, '') as chngrpname,t1.channel_uuid,NVL2 ( t3.channel_no||t3.chn_nm,t3.channel_no||'-'||t3.chn_nm, '') as chnname,t1.priority");		
		sb.append( " from ");
		sb.append(Table.T_APP_INF_CHN_GRP);
		sb.append(" t1 LEFT JOIN ");
		sb.append(Table.T_APP_INF_CHANNEL_GRP);
		sb.append(" t2 on t1.channel_grp_uuid=t2.channel_grp_uuid ");
		sb.append(" LEFT JOIN ");
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t3 on t1.channel_uuid=t3.channel_uuid ");
		sb.append(" where t1.rec_st!='");
		sb.append(SystemConstant.DEL_STATUS);
		sb.append("' and t1.chn_grp_uuid='");
		sb.append(chn_grp_uuid+"'");
		return infChnGrpDao.queryForMap(sb.toString());
		
	}
	
	/**
	 * 根据记录id集合删除数据
	 * @param chn_grp_uuids
	 */
	public void deleteInfChnGrpByIds(String chn_grp_uuids) {
		infChnGrpDao.deleteByStatus("chn_grp_uuid",chn_grp_uuids);
	}
	
	
	/**
	 * 校验通道分组
	 * @param infChnGrp
	 * @return
	 */
	public String checkChnGrp(App_Inf_Chn_GrpEntity infChnGrp){
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.chn_grp_uuid,t1.channel_grp_uuid,t1.channel_uuid from ");		
		sb.append(Table.T_APP_INF_CHN_GRP);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(infChnGrp.getChannel_grp_uuid())) {
			sb.append( " and t1.channel_grp_uuid = ? " );
			list.add(infChnGrp.getChannel_grp_uuid());
		}	
		if (!StringUtils.isBlank(infChnGrp.getChannel_uuid())) {
			sb.append( " and t1.channel_uuid = ? " );
			list.add(infChnGrp.getChannel_uuid());
		}	
		if (!StringUtils.isBlank(infChnGrp.getChn_grp_uuid())) {
			sb.append( " and t1.chn_grp_uuid != ? " );
			list.add(infChnGrp.getChn_grp_uuid());
		}		
		List result=infChnGrpDao.queryForList(sb.toString(), list);
		if(result==null||result.size()==0){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}

}
